package com.yyk.yiliao.ui.activity.shopcart;

import android.content.Context;
import androidx.annotation.NonNull;
import android.view.View;

import com.bumptech.glide.Glide;
import com.ocnyang.cartlayout.CartAdapter;
import com.ocnyang.cartlayout.viewholder.CartViewHolder;
import com.yyk.yiliao.R;
import com.yyk.yiliao.util.rx.ApiService;

import java.util.List;

public class ShopCart_Adapter extends CartAdapter<CartViewHolder> {


    public ShopCart_Adapter(Context context, List datas) {
        super(context, datas);
    }

    @Override
    protected CartViewHolder getNormalViewHolder(View itemView) {
        return new NormalViewHolder(itemView, -1);
    }

    @Override
    protected CartViewHolder getGroupViewHolder(View itemView) {
        return (CartViewHolder) new GroupViewHolder(itemView, R.id.checkbox);
    }

    @Override
    protected CartViewHolder getChildViewHolder(View itemView) {
        return (CartViewHolder) (new ChildViewHolder(itemView, R.id.checkbox) {
            @Override
            public void onNeedCalculate() {
                if (onCheckChangeListener != null) {
                    onCheckChangeListener.onCalculateChanged(null);
                }
            }
        });
    }

    @Override
    protected int getChildItemLayout() {
        return R.layout.activity_main_item_child;
    }

    @Override
    protected int getGroupItemLayout() {
        return R.layout.activity_main_item_group;
    }

    @Override
    protected int getNormalItemLayout() {
        return R.layout.activity_main_item_normal;
    }

    @Override
    public void onBindViewHolder(@NonNull CartViewHolder holder, final int position) {
        super.onBindViewHolder(holder, position);
        if (holder instanceof ChildViewHolder) {
            final ChildViewHolder childViewHolder = (ChildViewHolder) holder;
            final GoodsBean goodsBean = (GoodsBean) mDatas.get(position);
            if (goodsBean != null) {
                childViewHolder.textView.setText(((GoodsBean) mDatas.get(position)).getGoods_name());
                childViewHolder.textViewPrice.setText(
                        mContext.getString(R.string.rmb_X, ((GoodsBean) mDatas.get(position)).getGoods_price()));
                childViewHolder.textViewNum.setText(String.valueOf(((GoodsBean) mDatas.get(position)).getGoods_amount()));
                //设置图片
                Glide.with(mContext).load(ApiService.BASE_URL + ((GoodsBean) mDatas.get(position)).getImg()).into(childViewHolder.drawGoods);
                /**
                 *
                 * 点击事件 商品id
                 */
                childViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        listenr.onClickItem((int) ((GoodsBean) mDatas.get(position)).getItemId());
                    }
                });
                /**
                 * 添加和删除
                 */
                childViewHolder.textViewReduce.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int num = Integer.valueOf(childViewHolder.textViewNum.getText().toString()).intValue();
                        if (num > 1) {
                            num--;
                            (((GoodsBean) mDatas.get(position))).setGoods_amount(num);
                            childViewHolder.textViewNum.setText(String.valueOf(num));
                            listenr.onClickAddDelete(num, position);
                            notifyDataSetChanged();

                        }
                    }
                });
                childViewHolder.textViewAdd.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int num = Integer.valueOf(childViewHolder.textViewNum.getText().toString()).intValue();
                        num++;
                        (((GoodsBean) mDatas.get(position))).setGoods_amount(num);
                        childViewHolder.textViewNum.setText(String.valueOf(num));
                        listenr.onClickAddDelete(num, position);
                        notifyDataSetChanged();

                    }
                });
            }


        } else if (holder instanceof GroupViewHolder) {
            GroupViewHolder groupViewHolder = (GroupViewHolder) holder;
            groupViewHolder.textView.setText(((ShopBean) mDatas.get(position)).getShop_name());
        } else if (holder instanceof NormalViewHolder) {
            NormalViewHolder normalViewHolder = (NormalViewHolder) holder;
            normalViewHolder.imgViewClose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mDatas.remove(position);
                    notifyItemRemoved(position);
                    notifyItemRangeChanged(position, mDatas.size());
                }
            });
            normalViewHolder.textView.setText(mContext.getString(R.string.normal_tip_X,
                    ((NormalBean) mDatas.get(position)).getMarkdownNumber()));
        }

    }

    /**
     * 增加和减少 回调接口
     */
    onListenr listenr;

    public void setOnListenr(onListenr listenr) {
        this.listenr = listenr;
    }

    public interface onListenr {
        void onClickAddDelete(int num, int position);

        void onClickItem(int id);
    }
}
